package com.winchain.chainbill.risk.biz.access.cmd;

import com.shell.bee.acct.AcctException;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.receivables.base.po.ArOperation;
import com.winchain.chainbill.risk.base.po.RmAccessQuota;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AccessTranCmd implements ICommand<RmAccessQuota> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctService billAcctService;

	public boolean execute(CommandContext<RmAccessQuota> context) throws Exception {
		boolean timeout = ((Boolean) context.getValue("Timeout")).booleanValue();
		if (!timeout) {
			AcctTxRec tran = getAcctTxRec(context);
			this.tradeService.fire(tran);

			context.setValue("txSn", tran.getTxSn());
		}
		return false;
	}

	private AcctTxRec getAcctTxRec(CommandContext<RmAccessQuota> context) {
		RmAccessQuota rmAccessQuata = (RmAccessQuota) context.getSource();

		String bizType = ((ArOperation) context.getValue("Operation")).getBizType();
		String finId = "";
		CusRec fin = (CusRec) context.getValue("Fin");
		if (fin != null) {
			finId = fin.getCusId();
		}
		AcctTxRec tranRcd = new AcctTxRec();
		tranRcd.setTxId(SystemUtil.getUUID());

		BigDecimal amount = rmAccessQuata.getAmount();
		String abs = "������������";

		String tranEntId = finId;
		String cpId = rmAccessQuata.getSellerId();

		String tranAcctNo = getAcctBas(tranEntId).getAcctNo();
		tranRcd.setTxAcctNo(tranAcctNo);
		tranRcd.setTxAmt(amount);
		tranRcd.setTxDt(SysDateUtil.getSystemDate());
		tranRcd.setTxTs(SysDateUtil.getSystemDateTime());
		tranRcd.setTxAbs(abs);
		tranRcd.setTxType(bizType);

		tranRcd.setTxSts("I");

		AcctBas acct = getAcctBas(cpId);
		tranRcd.setCpAcctNo(acct.getAcctNo());
		tranRcd.setCpAcctNm(acct.getAcctNm());
		tranRcd.setCpBankNm(acct.getFiNm());
		tranRcd.setCurrCode("QUOTA");
		tranRcd.setTxCashFlag("N");
		tranRcd.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		tranRcd.setSrcCode(sourceCode);
		tranRcd.setBizId(rmAccessQuata.getAccId());
		tranRcd.setDataScLvl("B");
		return tranRcd;
	}

	private AcctBas getAcctBas(String custId) {
		String acctType = "1";
		AcctBas acct = this.billAcctService.getAcctBas(custId, acctType, null);
		if (acct == null) {
			throw new AcctException(custId + "-" + acctType + "����������");
		}
		return acct;
	}
}
